package net.xujialiang.XTestRPA.Service.RPA;

import net.xujialiang.XTestRPA.Entity.RPA.XRPAPoint;
import net.xujialiang.XTestRPA.Entity.RPA.XRPATaskDetail;
import net.xujialiang.XTestRPA.Repository.RPA.RPAPointRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.LinkedHashMap;
import java.util.Map;

@Service
public class XRPAPointService {
    @Autowired
    RPAPointRepository pointRepository;

    /**
     * 点数操作
     * @param taskId 任务ID
     * @param taskName 任务名称
     * @param point 点数
     * @param pointType 类型
     * @param createAccount 执行人
     * @param ttc 租户
     * @throws Exception
     */
    public void costPoint(String taskId, String taskName, int point,  String pointType, String createAccount, String ttc) throws Exception {
        XRPAPoint entity = XRPAPoint.builder()
                .taskID(taskId)
                .taskName(taskName)
                .point(point)
                .pointType(pointType)
                .executeBy(createAccount)
                .ttc(ttc)
                .createTime(new Timestamp(System.currentTimeMillis()))
                .build();
        pointRepository.save(entity);
    }

    /**
     * 分页获取点数数据
     * @param page 页码
     * @param pageSize 每页条数
     * @return
     */
    public Map<String, Object> fetchByPage(String ttc, int page, int pageSize) {
        Sort sort = Sort.by(Sort.Direction.DESC,  "id");
        PageRequest pr =  PageRequest.of(page,pageSize, sort);
        Page<XRPAPoint> pageData = pointRepository.findAllByTtc(ttc, pr);
        Map<String, Object> ret = new LinkedHashMap<>();
        ret.put("totalPages", pageData.getTotalPages());
        ret.put("totalCount", pageData.getTotalElements());
        ret.put("page", page);
        ret.put("pageSize", pageSize);
        ret.put("list", pageData.getContent());
        return ret;
    }
}
